package com.bawei.module_video.home.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.mvi.base.BaseViewModel
import com.bawei.module_video.home.intent.SimpleTypeIntent
import com.bawei.module_video.home.model.repository.HomeRepository
import com.bawei.module_video.home.state.SimpleTypeState
import com.bawei.module_video.home.state.VideoState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch


class HomeViewModel : BaseViewModel<SimpleTypeIntent>(){
    private val repository: HomeRepository by  lazy { HomeRepository() }

    private var simpleTypeFlow = MutableStateFlow<SimpleTypeState>(SimpleTypeState.Init)

    private var videoFlow = MutableStateFlow<VideoState>(VideoState.Init)

    val simpleTypeFlow_ = simpleTypeFlow.asStateFlow()

    val videoFlow_ = videoFlow.asStateFlow()
    //1:根据意图发起网络请求
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is SimpleTypeIntent.getSimpleType-> getSimpleTypeData()
                    is SimpleTypeIntent.getVideoType-> getVideoData(
                        channelId = it.channel,
                        page = it.page,
                        pageSize = it.pageSize
                    )
                }
            }
        }
    }
    //2:根据网络请求结果更新状态流
    private fun getSimpleTypeData() {
        viewModelScope.launch {
            repository.getSimpleType()
            .catch { e->
                simpleTypeFlow.value = SimpleTypeState.Failed(e.message.toString())
            }.filter {
                it != null
            }.collect{
                if(it.code == 200){
                    if(it.data.isEmpty()){
                        simpleTypeFlow.value = SimpleTypeState.Empty
                    }else{
                        simpleTypeFlow.value = SimpleTypeState.SimpleTypeSuccess(it.data)
                    }
                }else{
                    simpleTypeFlow.value = SimpleTypeState.Failed(it.msg)
                }
            }

        }
    }

    private fun getVideoData(channelId:String, page:Int,pageSize:Int) {
        viewModelScope.launch {
            repository.getVideo(channelId, page, pageSize)
                .catch { e->
                    videoFlow.value = VideoState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.isEmpty()){
                            videoFlow.value = VideoState.Empty
                        }else{
                            videoFlow.value = VideoState.VideoSuccess(it.data)
                        }
                    }else{
                        videoFlow.value = VideoState.Failed(it.msg)
                    }
                }

        }
    }

}